﻿using UnityEngine;

/// <summary>
/// 音频控制器
/// </summary>
public class AudioController : BaseController {

    static AudioController _inst;
    public static AudioController inst { get { return _inst; } }

    ///////////////////////////////////////////////////////////////////////////
    /// <summary>
    /// 声源
    /// </summary>
    public AudioSource _audioSource;

    /// <summary>
    /// UI点击音效
    /// </summary>
    public AudioClip _uiClickEffect;
    ///////////////////////////////////////////////////////////////////////////

    /// <summary>
    /// 音效音量
    /// </summary>
    float _effVolume;

    protected override void InitData()
    {
        base.InitData();

        _effVolume = 1.0f;

        if (LocalStorage.HasKey("音效音量"))
        {
            effectVolume = LocalStorage.GetFloat("音效音量");
        }
        else
        {
            LocalStorage.SetFloat("音效音量", effectVolume);
        }

        if (LocalStorage.HasKey("音乐音量"))
        {
            musicVolume = LocalStorage.GetFloat("音乐音量");
        }
        else
        {
            LocalStorage.SetFloat("音乐音量", musicVolume);
        }

        _inst = this;
    }

    /// <summary>
    /// 播放背景音乐
    /// </summary>
    /// <param name="clip"></param>
    /// <param name="loop"></param>
    public void PlayBGM(AudioClip clip, bool loop = true)
    {
        if (_audioSource.clip != clip)
        {
            _audioSource.clip = clip;
            _audioSource.loop = loop;
            _audioSource.Play();
        }
    }

    /// <summary>
    /// 播放背景音乐
    /// </summary>
    /// <param name="name"></param>
    /// <param name="loop"></param>
    public void PlayBGM(string name, bool loop = true)
    {
        PlayBGM(LoadClip(name), loop);
    }

    /// <summary>
    /// 播放音效
    /// </summary>
    /// <param name="clip"></param>
    public void PlayEffect(AudioClip clip)
    {
        if (_effVolume > 1.0f)
        {
            _effVolume = 1.0f;
            DebugInfo.Warning("当前音效音量过大");
        }
        _audioSource.PlayOneShot(clip, _effVolume);
    }

    /// <summary>
    /// 播放音效
    /// </summary>
    /// <param name="name"></param>
    public void PlayEffect(string name)
    {
        PlayEffect(LoadClip(name));
    }

    /// <summary>
    /// 播放UI点击音效
    /// </summary>
    public void PlayUIClickEffect()
    {
        PlayEffect(_uiClickEffect);
    }

    /// <summary>
    /// 设置/获取音乐音量
    /// </summary>
    public float musicVolume
    {
        get { return _audioSource.volume; }
        set { _audioSource.volume = value; }
    }

    /// <summary>
    /// 设置/获取音效音量
    /// </summary>
    public float effectVolume
    {
        get { return _effVolume; }
        set { _effVolume = value; }
    }

    /// <summary>
    /// 保存音效和音乐音量
    /// </summary>
    public void SavePreference()
    {
        LocalStorage.SetFloat("音效音量", effectVolume);
        LocalStorage.SetFloat("音乐音量", musicVolume);
    }

    /// <summary>
    /// 加载音效
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public static AudioClip LoadClip(string name)
    {
        return Resources.Load<AudioClip>("Audio/" + name);
    }
}
